package hust.ioic.oa.view.action;

import hust.ioic.oa.base.BaseAction;
import hust.ioic.oa.domain.Department;

import hust.ioic.oa.domain.MessageQuene;
import hust.ioic.oa.domain.Operator;
import hust.ioic.oa.domain.Rdfile;

import hust.ioic.oa.qilin.utils.GetDate;
import hust.ioic.oa.utils.MessageQueneInfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class MessageQueneAction extends BaseAction<MessageQuene> {
	// 消息列表
	public String list() {
		// 获得操作员信息
		Integer id = getCurrentOperator().getId();
		List<Rdfile> rdfiles = rdfileService.findAll();
		List<Operator> operatorList = operatorService.findAll();
		List<MessageQuene> mqsList = messageQueneService.getMBySender(id);
		List<MessageQuene> mqrList = messageQueneService.getMByRecevier(id);
		ActionContext.getContext().put("mqsList", mqsList);
		ActionContext.getContext().put("mqrList", mqrList);
		ActionContext.getContext().put("rdfiles", rdfiles);
		ActionContext.getContext().put("operatorList", operatorList);
		return "list";
	}

	// 删除消息
	public String delete() {
		messageQueneService.delete(model.getId());
		return "toList";
	}

	// 刷新
	public String fresh() {
		return "toList";
	}

	// 请求重新审阅保存方法
	public String requestAgain() {
		GetDate gd = new GetDate();
		MessageQuene mq = messageQueneService.getById(model.getId());

		mq.setSendTime(gd.getCurrentTime());
		mq.setStatus(4);
		mq.setRequestRemark(model.getRequestRemark());
		messageQueneService.update(mq);
		return "toList";
	}

	// 请求重新审阅页面
	public String editUI() {
		MessageQuene mq = messageQueneService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(mq);
		return "saveUI";
	}

	// 审批列表
	public String checkList() {
		// 得到当前登录用户
		Operator operator = getCurrentOperator();
		// 得到当前用户需要处理的文件队列
		List<MessageQuene> messageQuene = messageQueneService
				.getMByRecevier(operator.getId());
		// 传到前台展示的list
		List<MessageQueneInfo> messageQueneInfoList = new ArrayList<MessageQueneInfo>();
		// 构造展示信息
		for (MessageQuene mq : messageQuene) {
			MessageQueneInfo mqi = new MessageQueneInfo();
			mqi.setId(mq.getId());
			mqi.setRdfileId(mq.getRdfileId());
			Rdfile rd = rdfileService.getById(mq.getRdfileId());
			mqi.setRdfileName(rd.getName());
			mqi.setRdfileType(rd.getType());

			Set<Department> departments = rd.getDepartments();
			StringBuilder sb = new StringBuilder();
			Iterator i = departments.iterator();// 先迭代出来
			while (i.hasNext()) {// 遍历
				Department dm = (Department) i.next();
				sb.append(dm.getName() + "、");
			}
			mqi.setDeparmentsName(sb.toString());
			Operator op = operatorService.getById(mq.getSendId());
			mqi.setSendName(op.getUsername());
			mqi.setSendTime(mq.getSendTime());
			messageQueneInfoList.add(mqi);
		}
		ActionContext.getContext().put("messageQueneInfoList",
				messageQueneInfoList);

		return "checkList";
	}

	// 审批对话框
	public String check() {
		MessageQuene mq = messageQueneService.getById(model.getId());
		// 准备回显
		ActionContext.getContext().getValueStack().push(mq);
		return "check";
	}

	// 保存审批结果
	public String add() {
		MessageQuene mq = messageQueneService.getById(model.getId());
		mq.setStatus(model.getStatus());
		mq.setReponseRemark(model.getReponseRemark());
		mq.setHandleTime(new GetDate().getCurrentTime()); // 更新处理时间
		messageQueneService.save(mq);
		// 审批意见为同意，则将文件设为可见
		if (model.getStatus().equals(1)) {
			Rdfile rd = rdfileService.getById(mq.getRdfileId());
			rd.setDisplay(1);
			rdfileService.save(rd);
		}

		return "toCheckList";
	}

}
